package MVC.view
{
	import MVC.model.ImageChangeModel;
	
	import View.UICanvas;
	
	import Vo.Image;
	
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class UICanvasmediator extends Mediator implements IMediator
	{
		private var urlArray:Array;
		public static var NAME:String="UICanvasmediator";
		//public static var Next_Click:String="Next_Click";
		//public static var Back_Click:String="Back_Click";
		public var current:int=0;
		public function UICanvasmediator(NAME:String=null, viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			(viewComponent as UICanvas).nextBtn.addEventListener(MouseEvent.CLICK,clickbackHandler);
			(viewComponent as UICanvas).backBtn.addEventListener(MouseEvent.CLICK,clickNextHandler);
			function clickbackHandler(e:MouseEvent):void
			{
				var len:int=urlArray.length;
				if(current==0)
				{
					current=len-1;
					(viewComponent as UICanvas).addBitmap(new Image(urlArray[current]));
					return;
				}
				else
				{
					current--;
					(viewComponent as UICanvas).addBitmap(new Image(urlArray[current]));
				}
			}
			function clickNextHandler(e:MouseEvent):void
			{
				var length:int=urlArray.length; 
				if(current==length-1)
				{
					current=0;
					(viewComponent as UICanvas).addBitmap(new Image(urlArray[current]));
					return;
				}
				else
				{
					current++;
					(viewComponent as UICanvas).addBitmap(new Image(urlArray[current]));
				}
			}
		}
		override public function listNotificationInterests():Array
		{
			var array:Array=new Array();
			array.push(ImageChangeModel.PushUrlComplete);
			return array;
		}
		override public function handleNotification(notification:INotification):void
		{
			if(!notification.getBody()) return;
			urlArray=notification.getBody() as Array;
			switch(notification.getName())
			{
				case ImageChangeModel.PushUrlComplete:
					(viewComponent as UICanvas).addBitmap(new Image(urlArray[0]));
					break;
			}
		}
	}
}